# _*_ coding:utf-8 _*_
from django.shortcuts import render, HttpResponseRedirect
import models
from mysite import settings
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
# Create your views here.


def index(request):
    term_list = models.Term.objects.all()
    link_list = models.Links.objects.all()
    post_all_list = models.Posts.objects.filter(status='0')
    paginator = Paginator(post_all_list, 6)
    page = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except PageNotAnInteger:
        post_list = paginator.page(1)
    except EmptyPage:
        post_list = paginator.page(paginator.num_pages)
    page_range = paginator.page_range
    print type(post_all_list)
    return render(request, 'index.html', {'term_list':term_list, 'post_list':post_list, 'page_range':page_range, 'link_list':link_list})


@login_required()
def create_post(request):
    if request.method == 'POST':
        content = request.POST['content']
        summary = request.POST['summary']
        term_id = request.POST['term']
        title = request.POST['title']
        status = request.POST['status']
        tag = request.POST['tag']
        author_id = request.user.id
        #imgname = request.FILES["img"]
        #upload_dir = "%s/%s" % (settings.BASE_DIR, settings.FileUploadDir)
        #with open('%s/%s' % (upload_dir, imgname), 'wb+') as destination:
        #    for chunk in imgname.chunks():
        #        destination.write(chunk)
        #head_img = '/static/img/upload/%s'% imgname
        add = models.Posts(title=title, summary = summary, content = content, term_id = term_id,status = status, author_id = author_id, tag = tag)
        add.save()
    term_list = models.Term.objects.all()
    return render(request, 'create_posts.html',{'term_list':term_list})


def show_post(request, post_id):
    term_list = models.Term.objects.all()
    post = models.Posts.objects.get(id=post_id)
    return render(request, 'post.html', {'post':post, 'term_list':term_list})


def term_search(request, sid):
    term_list = models.Term.objects.all()
    post_all_list = models.Posts.objects.filter(status='0').filter(term_id=sid)
    paginator = Paginator(post_all_list, 5)
    page = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except PageNotAnInteger:
        post_list = paginator.page(1)
    except EmptyPage:
        post_list = paginator.page(paginator.num_pages)
    page_range = paginator.page_range
    currnet_term = models.Term.objects.select_related().get(pk=sid)
    return render(request, 'search_result.html', {'term_list':term_list, 'post_list':post_list, 'page_range':page_range, 'currnet_term':currnet_term})


def search(request):
    term_list = models.Term.objects.all()
    if request.method == 'GET':
        search_type = request.GET['search_type']
        keyword = request.GET['keyword']
        if search_type == 'title':
            search_result_list = models.Posts.objects.filter(title__contains=keyword)
            search_type = u'标题'
        elif search_type == 'content':
            search_result_list = models.Posts.objects.filter(content__contains=keyword)
            search_type = u'内容'
        elif search_type == 'tag':
            search_result_list = models.Posts.objects.filter(tag__contains=keyword)
            search_type = u'标签'
        else:
            search_result_list = models.Posts.objects.filter(title__contains=keyword)
        paginator = Paginator(search_result_list, 5)
        page = request.GET.get('page')
        try:
            post_list = paginator.page(page)
        except PageNotAnInteger:
            post_list = paginator.page(1)
        except EmptyPage:
            post_list = paginator.page(paginator.num_pages)
        page_range = paginator.page_range
        search_content = keyword
        return render(request, 'search_result.html', {'term_list':term_list, 'post_list':post_list, 'page_range':page_range, 'search_content':search_content, 'search_type':search_type})


def acc_login(request):
    if request.method == 'GET':
        term_list = models.Term.objects.all()
        return render(request, 'login.html', {'term_list':term_list})
    else:
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            return HttpResponseRedirect("/")
        else:
            term_list = models.Term.objects.all()
            login_err = "用户名或者密码错误！"
            return render(request, 'login.html', {'term_list':term_list, 'login_err':login_err})


@login_required()
def acc_logout(request):
    logout(request)
    return HttpResponseRedirect("/")


def regist_user(request):
    term_list = models.Term.objects.all()
    return render(request, 'register.html', {'term_list':term_list})


def term_post_list(request, sid):
    term_list = models.Term.objects.all()
    post_all_list = models.Posts.objects.filter(status='0').filter(term_id=sid)
    paginator = Paginator(post_all_list, 5)
    page = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except PageNotAnInteger:
        post_list = paginator.page(1)
    except EmptyPage:
        post_list = paginator.page(paginator.num_pages)
    page_range = paginator.page_range
    currnet_term = models.Term.objects.select_related().get(pk=sid)
    return render(request, 'index.html', {'term_list':term_list, 'post_list':post_list, 'page_range':page_range, 'currnet_term':currnet_term})





